package com.auxgroup.smarthome.inner.controller;

import com.auxgroup.bridge.app.inner.dto.DeviceInfoWithTokenDto;
import com.auxgroup.bridge.app.inner.vo.AppDeviceInfoListVo;
import com.auxgroup.bridge.app.inner.vo.DeviceInfoNewVo;
import com.auxgroup.bridge.app.inner.vo.DeviceInfoVo;
import com.auxgroup.smarthome.inner.entity.DeviceInfoEntity;
import com.auxgroup.smarthome.inner.service.DeviceInfoDBService;
import com.auxgroup.smarthome.syscode.AppCode;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by fju on 2017/7/25.
 */
@Api(tags = "绑定与解绑", description = "App用户和设备相关")
@RestController
public class DeviceInfoController {

    @Autowired
    private DeviceInfoDBService deviceInfoDBService;

    @ApiOperation(value = "设备绑定",notes = "App用户和设备绑定 ")
    @RequestMapping(value = "/device_bindings", method = RequestMethod.POST)
    public ApiResponse<DeviceInfoNewVo> bindDevice(@RequestBody DeviceInfoWithTokenDto deviceInfoWithTokenDto) {
        return deviceInfoDBService.bindDevice(deviceInfoWithTokenDto);
    }

    @ApiOperation(value = "设备解绑",notes = "App用户和设备解绑 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "Saas 用户Id",  paramType = "query", dataType = "string",required = true),
            @ApiImplicitParam(name = "appId", value = "application Id",  paramType = "query", dataType = "string",required = true),
            @ApiImplicitParam(name = "token", value = "paas token",  paramType = "query", dataType = "string",required = true),
            @ApiImplicitParam(name = "deviceId", value = "设备Id",  paramType = "query", dataType = "string",required = true),
    })
    @RequestMapping(value = "/device_bindings", method = RequestMethod.DELETE)
    public ApiResponse unBindDevice(@RequestParam("userId") @NotEmpty(message = "用户id不能为空") String userId, @RequestParam("appId") @NotEmpty(message = "appid不能为空") String appId, @RequestParam("token") @NotEmpty(message = "token不能为空") String token, @RequestParam("deviceId") @NotEmpty(message = "设备id不能为空") String deviceId) {
        return deviceInfoDBService.unBindDevice(userId,appId,token,deviceId);
    }

    @ApiOperation(value = "获取app用户设备列表")
    @GetMapping(value = "/device_bindings/{uid}")
    public ApiResponse<List<AppDeviceInfoListVo>> getAppDeviceList(@PathVariable("uid") String uid) {
        return ApiResponse.ok(deviceInfoDBService.getAppDeviceList(uid));
    }

    @ApiOperation(value = "检查设备是否存在",notes = "检查设备 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备Id",  paramType = "query", dataType = "string",required = true),
    })
    @RequestMapping(value = "/device_exist", method = RequestMethod.GET)
    public ApiResponse<Boolean> exist(@RequestParam("deviceId") String deviceId){
        return deviceInfoDBService.exist(deviceId);
    }

    @ApiOperation(value = "获取设备DeviceId对应的Did",notes = "设备Did ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "did", value = "设备did",  paramType = "query", dataType = "string",required = true),
    })
    @RequestMapping(value = "/device_id", method = RequestMethod.GET)
    public ApiResponse<String> getMappedDeviceId(@RequestParam("did") String did){
        return deviceInfoDBService.getMappedDeviceId(did);
    }

    @ApiOperation(value = "获取设备Did对应的DeviceId",notes = "设备Did ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "did", value = "设备did",  paramType = "query", dataType = "string",required = true),
    })
    @RequestMapping(value = "/device_did", method = RequestMethod.GET)
    public ApiResponse<String> getMappedDid(@RequestParam("deviceId") String deviceId){
        return deviceInfoDBService.getMappedDid(deviceId);
    }

    @ApiOperation(value = "根据设备id返回设备型号的新旧")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId",value = "设备id",paramType = "query",dataType = "string",required = true)
    })
    @RequestMapping(value = "/device_manufacturer",method = RequestMethod.GET)
    public ApiResponse<Integer> getDeviceManufacturers(String deviceId){
        return deviceInfoDBService.getDeviceManufacturers(deviceId);
    }

    @ApiOperation(value = "修改设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mac",value = "mac地址",paramType = "path",dataType = "string",required = true),
            @ApiImplicitParam(name = "sn",value = "SN",paramType = "query",dataType = "string",required = true),
            @ApiImplicitParam(name = "alias",value = "设备别名",paramType = "query",dataType = "string"),
            @ApiImplicitParam(name = "uid",value = "用户uid",paramType = "query",dataType = "string"),
    })
    @RequestMapping(value = "/device/{mac}",method = RequestMethod.PUT)
    public ApiResponse updateDeviceInfo(@PathVariable("mac") String mac, String sn, String alias,String uid){
        if(StringUtils.isBlank(sn) && StringUtils.isBlank(alias)){
            return ApiResponse.prompt(AppCode.SN_ALIAS_EMPTY);
        }
        return deviceInfoDBService.updateDeviceInfo(uid,mac,sn,alias);
    }

    @ApiOperation(value = "获取设备列表[内部接口]")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skip",value = "跳过条数",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "limit",value = "限制条数",paramType = "query",dataType = "int",required = true)
    })
    @RequestMapping(value = "/devices",method = RequestMethod.GET)
    public ApiResponse<List<DeviceInfoEntity>> getDeviceList(@RequestParam("skip") Integer skip,@RequestParam("limit") Integer limit){
        return deviceInfoDBService.getDeviceList(skip,limit);
    }

    @ApiOperation(value = "获取设备总数")
    @RequestMapping(value = "/device_count",method = RequestMethod.GET)
    public ApiResponse<Integer> getDeviceCount(){
        return deviceInfoDBService.getDeviceCount();
    }


    @ApiOperation(value = "根据设备id返回设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId",value = "设备id",paramType = "query",dataType = "string",required = true)
    })
    @RequestMapping(value = "/device_info/{deviceId}",method = RequestMethod.GET)
    public ApiResponse<DeviceInfoVo> getDeviceInfo(@PathVariable("deviceId") String deviceId){
        return deviceInfoDBService.getDeviceInfo(deviceId);
    }

    @ApiOperation(value = "通过deviceId获取设备信息",notes = "返回的设备信息很简洁，可以通过该设备信息，识别新旧设备，识别是挂式还是立式，是商用还是家用")
    @RequestMapping(value = "/device_new_info/{deviceId}",method = RequestMethod.GET)
    public ApiResponse<DeviceInfoNewVo> getDeviceNewInfoByDeviceId(@PathVariable("deviceId") @NotBlank String deviceId){
       return deviceInfoDBService.getDeviceNewInfoByDeviceId(deviceId);
    }

    @ApiOperation(value = "通过did获取设备信息",notes = "返回的设备信息很简洁，可以通过该设备信息，识别新旧设备，识别是挂式还是立式，是商用还是家用")
    @RequestMapping(value = "/device_new_did_info/{did}",method = RequestMethod.GET)
    public ApiResponse<DeviceInfoNewVo> getDeviceNewInfoByDid(@PathVariable("did") @NotBlank String did){
        return deviceInfoDBService.getDeviceNewInfoByDid(did);
    }

    @ApiOperation(value = "通过mac获取设备信息",notes = "返回的设备信息很简洁，可以通过该设备信息")
    @GetMapping(value = "/device_mac_info/{mac}")
    public ApiResponse<DeviceInfoNewVo> getDeviceInfoNewVoByMac(@PathVariable("mac") @NotBlank String mac){
        return deviceInfoDBService.getDeviceInfoNewVoByMac(mac);
    }


    @ApiOperation(value = "modelItem内机物料获取productKey")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelItem",value = "内机物料",paramType = "query",dataType = "int",required = true),
    })
    @GetMapping(value = "/produckey/model_item")
    public ApiResponse<String> getProductKeyByModelItem(String modelItem){
        return deviceInfoDBService.getProductKeyByModelItem(modelItem);
    }



}
